<?php

$sql ='SELECT tab.url,a.title FROM article_info a  LEFT JOIN Table_125 tab ON 
a.id = tab.Field_21524   ORDER BY  a.created DESC LIMIT 0, 2  ';
$rows   = $db->getAll($sql);

if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_new_room.php');
class NewRoomControl
{
	var $new_room_m;
	function __construct()
	{
		$this->new_room_m = new NewRoomModule();
	}

	/**
	* 根据id得到faq_info_new信息
	* @param   int         $new_id
	* @return  array
	*/
	function  get_new_id_info($new_id)
	{
		
		$p_rows = $v_rows =  array();
		$sql = 'SELECT id,title,keyword,summary ,content,sub_title  FROM article_info  WHERE  id = '.$new_id;
		$rows =  $this->new_room_m->get_new_info($sql);
		$rows = $rows[0];
		return $rows;
	
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @return  array
	*/
	function get_url_info($page,$type = '',$pagesize = 10,$search_array = null )
	{
		$url_str = '';
		if($type == 'list')
		{ 
			$sql = "SELECT COUNT(*) AS total FROM article_info WHERE is_delete = 0  ";
		}
		else
		{
			$data = $this->get_search_condition($search_array,'get_url');
			$url_str = $data['url_str'];
			$sql = 'SELECT COUNT(*) AS total FROM article_info AS new  WHERE is_delete = 0   '.$data['where_str'];
		}
		$page_info = get_page_info($sql,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表new信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   array      $search_array   搜索数组
	* @return  array
	*/
	function  get_new_info($type,$start = 0,$pagesize = 10,$search_array = null)
	{
		$date =  array();
		if($type == 'list')
		{
			$sql = "SELECT id,title,keyword,summary ,author, created ,hits,order_id,
			update_user,IF(update_time > 0, FROM_UNIXTIME(update_time, '%Y-%m-%d %H:%s'), '') 
			update_time  FROM article_info WHERE is_delete = 0  ORDER BY id  DESC   LIMIT $start,$pagesize ";
		}else
		{
			$data = $this->get_search_condition($search_array,'search_info');
			$sql = "SELECT id,title,keyword,summary ,author, created ,hits,order_id,
			update_user,IF(update_time > 0, FROM_UNIXTIME(update_time, '%Y-%m-%d %H:%s'), '') 
			update_time FROM article_info  WHERE is_delete = 0   ".$data['where_str']." 
			ORDER BY id  DESC  LIMIT ".$start.','.$pagesize ;
		}
		return $this->new_room_m->get_new_info($sql);
	}
	/**
	* ajax改变值
	* @param   int      $id               FAQ文章id
	* @param   int      $change_value    值
	* @param   int      $field           要改变的数据字段
	* @return  int
	*/
	function  update_info($id,$change_value,$field)
	{
		$sql = "UPDATE article_info  SET $field = '$change_value' WHERE id = $id ";
		return $this->new_room_m->change_info($sql);
	}
	/**
	* 批量删除
	*
	* @param   string         $list_id    id字符串
	*
	* @return  int
	*/
	function delete_new($list_id)
	{
		$sql = "UPDATE article_info SET is_delete = 1  WHERE id IN ($list_id) ";
		return $this->new_room_m->change_info($sql);
	}
	/**
	* ajax改变值
	* @param   int      $id               FAQ文章id
	* @param   int      $change_value    值
	* @param   int      $field           要改变的数据字段
	* @return  int
	*/
	function  change_info($id,$change_value,$field)
	{
		$sql = "UPDATE article_info SET $field = '$change_value' WHERE id = $id ";
		return $this->new_room_m->get_new_info($sql);
	
	}
	/**
	* 得到在搜索的条件
	*
	* @param   array        $search_array   搜索条件
	* @return  array
	*/
	function get_search_condition($search_array,$type = '')
	{
		$url_str = '&url_page=1'; $where_str = '';
		if(!empty($search_array['new_id']))
		{
			$where_str = ' AND  id = '.$search_array['new_id'];
			$url_str.= '&new_id='.$search_array['new_id'];
		}else
		{ 
			if(!empty($search_array['title']))//标题
			{
			$where_str .= " AND title LIKE '%{$search_array['title']}%'";
			$url_str.= '&new_title='.urlencode($search_array['title']);
			}
			if(!empty($search_array['created_date']))//日期
			{
			$created_s_date = date('Y-m-d H:i:s',strtotime($search_array['created_date'].'-01-01'));
			$created_e_date = date('Y-m-d H:i:s',strtotime(($search_array['created_date']+1).'-01-01'));
			$where_str .= " AND created > '{$created_s_date}'";
			$where_str .= " AND created < '{$created_e_date}'";
			$url_str.= '&created_date='.$search_array['created_date'];
			}
		}
		if($type == 'get_url')
		{
			return array('where_str' => $where_str,'url_str' => $url_str);
		}else
		{
			return array('where_str' => $where_str);
		}
	
	}
	/**
	* 增加或者修改new
	*
	* @param   string      $type            操作类型
	* @param   string      array     	      
	* @param   text        $content         内容
	* @param   int         $ad_id           广告id
	* @return  int
	*/
	function  operate_new($type,$array = '')
	{
		$operate_id = 0;
		if($type == 'add')
		{
			$sql = "INSERT INTO  article_info(type_id,title,sub_title,keyword,summary,content,author,created,update_user,update_time ) 
			VALUES  ('1','{$array['title']}','{$array['sub_title']}','{$array['keyword']}','{$array['summary']}','{$array['content']}',
			'".$_SESSION['AName']."','".date('Y-m-d H:i:s',time())."','".$_SESSION['AName']."','".$_SERVER['REQUEST_TIME']."')";
			
			$GLOBALS['db']->query($sql);
			$operate_id = $GLOBALS['db']->insert_id();
		}
		elseif($type == 'update')
		{
			$sql = "UPDATE article_info SET title = '{$array['title']}',sub_title = '{$array['sub_title']}',keyword = '{$array['keyword']}',summary = '{$array['summary']}',
			content = '{$array['content']}' ,update_user = '".$_SESSION['AName']."',
			update_time =  '".$_SERVER['REQUEST_TIME']."' WHERE id = ".$array['id'];
			$operate_id =  $GLOBALS['db']->query($sql);
		
		}
		return $operate_id;
	
	}

}
?>